package org.jbrisk.IO;

import java.io.File;

import org.jbrisk.exceptions.NullArgumentException;
import org.jbrisk.text.StringUtils;

/**
 * Provides helper methods related with paths and files.
 * 
 * @author Rafael
 * 
 */
public class Path {

	/**
	 * Returns the file name without the extension.
	 * 
	 * @param file
	 *            File with extension.
	 * @return File name without extension.
	 */
	public static String getFileNameWithoutExtension(File file) {

		return getFileNameWithoutExtension(file.getName());
	}

	/**
	 * Returns the file name without the extension.
	 * 
	 * @param fileName
	 *            File name with extension.
	 * @return File name without extension.
	 */
	public static String getFileNameWithoutExtension(String fileName) {

		String fileNameWithoutExt = fileName;

		int lastDotIdx = fileName.lastIndexOf('.');

		if (lastDotIdx > -1) {

			fileNameWithoutExt = fileName.substring(0, lastDotIdx);
		}

		return fileNameWithoutExt;
	}

	/**
	 * Combines the two path parts.
	 * 
	 * @param path1
	 *            First path part.
	 * @param path2
	 *            Second path part.
	 * @return Combined path.
	 */
	public static String combine(String path1, String path2) {

		if (StringUtils.isNullOrEmpty(path1))
			throw new NullArgumentException("path1");

		if (StringUtils.isNullOrEmpty(path2))
			throw new NullArgumentException("path2");

		// Clean paths
		if (path1.endsWith(File.separator))
			path1 = path1.substring(0, path1.length() - 1);

		if (path2.startsWith(File.separator))
			path2 = path2.substring(1);

		return path1 + File.separator + path2;
	}
}
